#!/usr/bin/env python
# -*- coding: utf-8 -*-
# by vellhe 2017/7/9
from flask import request
from flask import Flask
from answer_search import AnswerSearcher
from question_classifier import QuestionClassifier
from question_parser import QuestionPaser
from gevent import pywsgi

# Flask初始化参数尽量使用你的包名，这个初始化方式是官方推荐的，官方解释：http://flask.pocoo.org/docs/0.12/api/#flask.Flask
app = Flask(__name__)


@app.route('/HelloWorld')
def hello_world():
    print('请求方式为------->', request.method)
    name=request.args.get("name")
    print(name)
    handler = ChatBotGraph()
    # 测试-start
    # problems=["离散结构的知识单元","离散结构属于什么","数学基础的知识点","离散结构的先修课程","递归的掌握程度","递归的重要程度","李四的职称","李四老师多大","李四老师性别","张三的学号","张三同学的性别","张三同学多大","张三上大几","张三选修的课程","李四讲授的课程","张三什么专业的","李四管理的学生",]
    # problems=["离散结构的知识单元有哪些","离散结构的简介","离散结构的学时是多少","操作系统的先修课程有什么","离散结构的教学大纲","离散结构的知识点有哪些",
    #           "离散结构属于什么专业","功能组织掌握程度","功能组织重要程度","递归的前置知识点",
    #           "递归的后继知识点","程序设计基础的前置知识单元","程序设计基础的后继知识单元","操作系统的后继课程","操作系统的前置课程","建模基础涵盖哪些知识点",
    #           "建模基础属于什么课程","离散结构有多少知识单元？","离散结构有多少知识点？"]
    print("这是chatbot脚本\n")
    # problems = "离散结构的知识点有哪些"
    problems = "离散结构属于的课程"
    an=handler.chat_main(problems)
    print(an)
    print("测试结束")
    return str(an)



class ChatBotGraph:
    def __init__(self):
        self.classifier = QuestionClassifier()
        self.parser = QuestionPaser()
        self.searcher = AnswerSearcher()

    def chat_main(self, sent):
        answer = '没能理解您的问题，我数据量有限，仍在扩充中...'
        res_classify = self.classifier.classify(sent)

        # print('类别：',res_classify)
        res_sql = self.parser.parser_main(res_classify)
        # print('sql语句',res_sql)

        final_answers = self.searcher.search_main(res_sql)
        return final_answers



if __name__ == "__main__":
    # 这种是不太推荐的启动方式，我这只是做演示用，官方启动方式参见：http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application
    #app.run(debug=True)
    server = pywsgi.WSGIServer(('127.0.0.1', 5000), app)
    server.serve_forever()

